package com.motion.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.motion.dto.*;
import com.motion.generator.entity.*;
import com.motion.generator.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class UserCourseService {
    @Autowired(required = false)
    UserCourseMapper userCourseMapper;
    @Autowired(required = false)
    CourseClassMapper courseClassMapper;
    @Autowired(required = false)
    CourseMapper courseMapper;
    @Autowired
    CourseLinkService courseLinkService;
//    public UserCourseListDto userCourseService(long userId){
//        List<UserCourseDto> first=null;
//        List<Long> typeNumber=null;
//        List<CourseClass> type=null;
//        UserCourseDto userCourseDto=null;
//        UserCourseListDto answer=null;
//        QueryWrapper<UserCourse> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("UserID",userId);
//        //找到所有课程
//        List<UserCourse> name=userCourseMapper.selectList(queryWrapper);
//        for (int t=0;t<name.size();t++){
//            userCourseDto=first.get(t);
//            //课程类型转化
//            typeNumber=conversion(userCourseDto.getLabels());
//            type=null;
//            for (int t2=0;t2<typeNumber.size();t2++){
//                type.add(courseClassMapper.selectById(type.get(t2)));
//            }
//            userCourseDto.setUserCourse(type);
//            first.add(userCourseDto);
//        }
//        answer.setTotal(name.size());
//        answer.setUserCourseDto(first);
//        return answer;
//    }
//    //类型string字符转化为List《int》列表
//    public List<Long> conversion(String str){
//        List<Long> answer=null;
//        for (int t=0;t<str.length();t++){
//             answer.add(Long.valueOf(str.split(";")[t]));
//        }
//        return answer;
//    }

    public IPage<UserCourse> getResentWatched(Long userId, Long page, Long size){
        //返回最近观看的5个课程
        QueryWrapper<UserCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId).ge("playCount", 1).orderByDesc("lastWatchTime");
        IPage<UserCourse> userCourseIPage = userCourseMapper.selectPage(new Page<>(page, size), queryWrapper);
        return userCourseIPage;
    }

    public IPage<UserCourse> getCollected(Long userId, Long page, Long size){
        //返回最近观看的5个课程
        QueryWrapper<UserCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId).eq("hasCollected", 1).orderByDesc("lastWatchTime");
        IPage<UserCourse> userCourseIPage = userCourseMapper.selectPage(new Page<>(page, size), queryWrapper);
        return userCourseIPage;
    }



    //用户收藏
    public CourseAbbDto userHasCollected(Long userId, Long page, Long size){
        QueryWrapper<UserCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userId);
        wrapper.eq("hasCollected", 1);
        wrapper.orderByDesc("lastWatchTime");
        List<UserCourse> userCourses= userCourseMapper.selectList(wrapper);
        Long num;
        CourseAbbDto courseAbbDto = new CourseAbbDto();
        courseAbbDto.setHasNext(userCourses.size() > size*page);

        List<CourseAbb> courseList = new ArrayList<>();
        for (UserCourse userCourse:userCourses){
            Course course = courseMapper.selectById(userCourse.getCourseId());

            CourseAbb courseAbb=new CourseAbb();
            courseAbb.setCourseId(course.getCourseId());
            courseAbb.setCourseName(course.getCourseName());
            courseAbb.setBackgroundUrl(course.getBackgroundUrl());
            courseAbb.setTargetAge(course.getTargetAge());
            courseAbb.setOnline(course.getOnLine());

//            List<String> stringList= Arrays.asList(course.getLabels().split(";").clone());
//            List<String> value=new ArrayList<>();
//            for (String idStr:stringList){
//                value.add(courseClassMapper.selectById(Long.valueOf(idStr)).getClassValue());
//            }
            courseAbb.setLabels(courseLinkService.getCourseLabel(course.getCourseId()));
            courseList.add(courseAbb);
        }
        courseAbbDto.setCourseList(courseList);
        return courseAbbDto;
    }

    //我的预定
    public CourseAbbDto userHasBooked(Long userId, Long page, Long size){
        QueryWrapper<UserCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userId);
        wrapper.eq("hasBooked", 1);
        wrapper.orderByDesc("lastWatchTime");
        List<UserCourse> userCourses= userCourseMapper.selectList(wrapper);
        CourseAbbDto courseAbbDto = new CourseAbbDto();
        courseAbbDto.setHasNext(userCourses.size() > size*page);

        List<CourseAbb> courseList = new ArrayList<>();
        for (UserCourse userCourse:userCourses){
            Course course = courseMapper.selectById(userCourse.getCourseId());

            CourseAbb courseAbb=new CourseAbb();
            courseAbb.setCourseId(course.getCourseId());
            courseAbb.setCourseName(course.getCourseName());
            courseAbb.setBackgroundUrl(course.getBackgroundUrl());
            courseAbb.setTargetAge(course.getTargetAge());
            courseAbb.setOnline(course.getOnLine());

//            List<String> stringList= Arrays.asList(course.getLabels().split(";").clone());
//            List<String> value=new ArrayList<>();
//            for (String idStr:stringList){
//                value.add(courseClassMapper.selectById(Long.valueOf(idStr)).getClassValue());
//            }
            courseAbb.setLabels(courseLinkService.getCourseLabel(course.getCourseId()));
            courseList.add(courseAbb);
        }
        courseAbbDto.setCourseList(courseList);
        return courseAbbDto;
    }
    //获取用户看过课程数
    public int getUserCoursecount(Long userId){
        QueryWrapper<UserCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.ge("playCount",0);
        List<UserCourse> userCourseList=userCourseMapper.selectList(queryWrapper);
        int rufues=0;
        for (UserCourse item:userCourseList){
            rufues =rufues+item.getPlayCount();
        }
        return rufues;
    }

    public boolean collectCourse(Long userId, Long courseId, int flag){
        QueryWrapper<UserCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("courseId", courseId);
        UserCourse userCourse = userCourseMapper.selectOne(queryWrapper);
        QueryWrapper<Course> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("courseId",courseId);
        Course tem=courseMapper.selectOne(queryWrapper1);
        if (tem==null){
            return false;
        }
        if(userCourse==null){
            //用户操作的是陌生的课程，则先创建一条记录
            UserCourse uc= new UserCourse();
            uc.setUserId(userId);
            uc.setCourseId(courseId);
            uc.setHasCollected(flag);
            userCourseMapper.insert(uc);
            return true;
        }
        //用户已对课程有过操作，直接修改表中数据即可
        userCourse.setHasCollected(flag);
        userCourseMapper.update(userCourse, queryWrapper);
        return true;
    }
    public boolean bookCourse(Long userId, Long courseId, int flag){
        QueryWrapper<UserCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("courseId", courseId);
        UserCourse userCourse = userCourseMapper.selectOne(queryWrapper);
        Course course=courseMapper.selectById(courseId);
        QueryWrapper<Course> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("courseId",courseId);
        Course tem=courseMapper.selectOne(queryWrapper1);
        if (tem==null){
            return false;
        }
        if(course.getOnLine()==1){
            return false;
        }
        if(userCourse==null){
            //用户操作的是陌生的课程，则先创建一条记录
            UserCourse uc= new UserCourse();
            uc.setUserId(userId);
            uc.setCourseId(courseId);
            uc.setHasBooked(flag);
            userCourseMapper.insert(uc);
            return true;
        }
        //用户已对课程有过操作，直接修改表中数据即可
        userCourse.setHasBooked(flag);
        userCourseMapper.update(userCourse, queryWrapper);
        return true;
    }

    public void updataUserCourse(Long useId,Long courseId){
        QueryWrapper<UserCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("useId",useId);
        queryWrapper.eq("courseId",courseId);
        UserCourse userCourse=userCourseMapper.selectOne(queryWrapper);
        if(userCourse!=null){
            userCourse.setPlayCount(userCourse.getPlayCount()+1);
            userCourse.setLastWatchTime(LocalDateTime.now());
            userCourseMapper.updateById(userCourse);
        }else {
            userCourse.setUserId(useId);
            userCourse.setCourseId(courseId);
            userCourse=new UserCourse();
            userCourse.setPlayCount(1);
            userCourse.setLastWatchTime(LocalDateTime.now());
            userCourseMapper.insert(userCourse);
        }
    }
    public List<UserCourse> course2userCourse(Long courseId){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("courseId",courseId);
        return userCourseMapper.selectList(queryWrapper);
    }
    public boolean delectByList(List<Long> longList){
        try {
            userCourseMapper.deleteBatchIds(longList);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
